/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: MENG
 * Date: 2022-07-11
 * Time: 22:54
 */
/*
 * 模拟实现循环链表
 * */
public class MyCircularQueue {
    public int[] elem;
    public int usedSize;
    public int front;//队头下标
    public int rear;//队尾下标


    public MyCircularQueue(int k) {
        this.elem = new int[k+1];
    }

    //判断元素是否满了，就判断rear后一个元素是不是front
    public boolean isFull() {
        return (rear+1)% elem.length == front;
    }
    //判断现在循环链表是不是空的，就看rear是否等于front，满了是rear后一个元素是否是front
    public boolean isEmpty() {
        return rear == front;
    }
    //入队
    public boolean enQueue(int value){
        if(isFull()){
            return false;
        }
        this.elem[rear] = value;
        //千万不能rear++
        rear = (rear+1)%elem.length;
        usedSize++;
        return true;
    }
    //出队
    public boolean deQueue(){
        if(isEmpty()){
            return false;
        }
        front = (front + 1) % elem.length;
        this.usedSize--;
        return true;
    }
    //获取队头元素
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }

    //获取队尾元素
    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        int index = (rear == 0)? elem.length - 1 : rear-1;
        return elem[index];
    }

}